/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * BaseFlowSkipController
 * 流程跳过
 * 修改纪录
 * 2021-02-23 版本：1.0 zhanglongfei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：zhanglongfei
 * 创建日期：2021-02-23
 */
package com.topscomm.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.controller.auto.BaseFlowSkipControllerAuto;
import com.topscomm.base.pojo.BaseFlowSkipInsEntity;
import com.topscomm.base.service.IBaseFlowSkipInsService;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.ParameterCache;

/**
 * @author: zhanglongfei
 * @date: 2021-02-23
 * @description:流程跳过Controller
 * @modify:
 */
@RestController
@RequestMapping("base/flowSkip")
public class BaseFlowSkipController extends BaseFlowSkipControllerAuto implements IBaseFlowSkipController {
	@Autowired
	IBaseFlowSkipInsService baseFlowSkipInsService;

	/**
	 * @author: zhanglongfei
	 * @date: 2021-02-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_parentid")) {
			String value = paraMap.get("qry_parentid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and flowid = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @description: 参照查询方法
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021-02-23
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description: 树查询方法
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021-02-23
	 * @modify:
	 */
	@GetMapping(value = "/queryTreeList")
	public ResponseResult<Object> queryTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		String sqlWhere = this.spellListSql(paraMap);
		sqlWhere += " order by id asc";
		List<Map<String, Object>> listModule = this.service.queryMapByWhere(sqlWhere);
		List<Map<String, Object>> listNewModule = new ArrayList<Map<String, Object>>();
		listNewModule.addAll(listModule);
		TreeModel treeModel = new TreeModel();
		List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "流程跳过树");
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("dataList", treeList);
		return ResponseResult.ok(dataMap);
	}

	/**
	 * @description:查询当前流程可以跳过的节点
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年2月26日下午3:10:16
	 * @modify:
	 */
	@GetMapping(value = "/queryFlowSkip")
	public ResponseResult<Object> queryFlowSkip(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String flowId = ConvertUtil.convertToString(paraMap.get("flowid"));
			String sourceType = ConvertUtil.convertToString(paraMap.get("sourcetype"));
			String sourceId = ConvertUtil.convertToString(paraMap.get("sourceid"));
			List<Map<String, Object>> list = service.queryMapByWhere("flowid='" + flowId + "' and enabled=1 order by sortcode,id");
			if (!StringUtil.isEmpty(sourceType) && !StringUtil.isEmpty(sourceId)) {
				List<BaseFlowSkipInsEntity> insList = baseFlowSkipInsService.queryByWhere("sourcetype='" + sourceType + "' and sourceid='" + sourceId + "'");
				for (Map<String, Object> map : list) {
					if (insList.stream().anyMatch(m -> m.getSkipcode().equals(ConvertUtil.convertToString(map.get("code"))))) {
						map.put("skiped", true);
					}
				}
			}
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:保存数据
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年2月26日下午5:47:43
	 * @modify:
	 */
	@PostMapping(value = "/saveData")
	public ResponseResult<Object> saveData(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			service.saveData(paraMap);
			return ResponseResult.ok("保存成功");
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:当前节点是否可以跳过
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年3月2日上午10:49:59
	 * @modify:
	 */
	@GetMapping(value = "/queryNodeCouldSkip")
	public ResponseResult<Object> queryNodeCouldSkip(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			String nodeInsId = ConvertUtil.convertToString(paraMap.get("nodeinsid"));
			boolean could = service.nodeCouldSkip(nodeInsId);
			String approvalOpinion = ParameterCache.getValue("McrApprovalUserSkipOpinion", "【无需办理】");
			Map<String, Object> result = new HashMap<>();
			result.put("could", could);
			result.put("opinion", approvalOpinion);
			return ResponseResult.ok(result);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}
}
